public class LinkedListReversal {
   public static void main(String[] args) {
      // 创建示例链表: 1->2->3->4->5
      Node head = new Node(1);
      head.next = new Node(2);
      head.next.next = new Node(3);
      head.next.next.next = new Node(4);
      head.next.next.next.next = new Node(5);

      System.out.println("原始链表:");
      print(head);
      Node reverse = reverse(head);
      System.out.println("反转后链表:");
      print(reverse);
   }



   public static Node reverse(Node T){
      Node pre =null;
      Node current = T;
      Node next = null;
      while (current != null ){
         //保存下一个节点
         next =current.next;
         //链表指针偏移
         current.next = pre;
         pre =current;
         //偏移遍历
         current =next;
      }
      //返回新的head
      return pre;
   }

   public static void print(Node head){
      Node current = head;
      while (current != null){
         System.out.println(current.data+"->");
         current = current.next;
      }
   }

}

class Node{
   int data;
   Node next;

   public Node(int data) {
      this.data = data;
   }

}
